package databinding;

import java.util.HashMap;
import java.util.List;

import model.ChiTietDichGia_Sach;
import model.ChiTietNXB_Sach;
import model.ChiTietTacGia_Sach;
import model.Sach;

import org.hibernate.criterion.Restrictions;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.Selectors;

import dao.entity.ChiTietDichGia_SachDAO;
import dao.entity.ChiTietNXB_SachDAO;
import dao.entity.ChiTietTacGia_SachDAO;

public class ChiTietSachViewModel {
	private String tenTacGia;
	private String tenDichGia;
	private String tenNhaXuatBan;

	public String getTenTacGia() {
		return tenTacGia;
	}

	public String getTenDichGia() {
		return tenDichGia;
	}

	public String getTenNXB() {
		return tenNhaXuatBan;
	}

	Sach sach = new Sach();

	// @Init
	// public void init(@ContextParam(ContextType.VIEW) Component view,
	// @ExecutionArgParam("image") String hinh,
	// @ExecutionArgParam("tenSach") String tenSach) {
	// Selectors.wireComponents(view, this, false);
	// this.hinh = hinh;
	// this.tenSach = tenSach;
	//
	// }
	@Init
	public void init(@ContextParam(ContextType.VIEW) Component view) {
		Selectors.wireComponents(view, this, false);
		Sach sach2 = (Sach) Sessions.getCurrent().getAttribute("sach");
		sach = sach2;

		// get ten tac gia
		ChiTietTacGia_SachDAO daoTG = new ChiTietTacGia_SachDAO();
		List<ChiTietTacGia_Sach> tenTG = daoTG.findByCriteria(Restrictions.eq(
				"sach", sach2));
		for (int i = 0; i < tenTG.size(); i++) {
			tenTacGia = tenTG.get(0).getTacGia().getTenTacGia();

		}
		// get ten nha xuat ban
		ChiTietNXB_SachDAO daoNXB = new ChiTietNXB_SachDAO();
		List<ChiTietNXB_Sach> tenNXB = daoNXB.findByCriteria(Restrictions.eq(
				"sach", sach2));
		for (int i = 0; i < tenNXB.size(); i++) {
			tenNhaXuatBan = tenNXB.get(i).getNxb().getTenNXB();

		}
		// get ten dich gia
		ChiTietDichGia_SachDAO daoDichGia = new ChiTietDichGia_SachDAO();
		List<ChiTietDichGia_Sach> tenDG = daoDichGia
				.findByCriteria(Restrictions.eq("sach", sach2));
		for (int i = 0; i < tenDG.size(); i++) {
			tenDichGia = tenDG.get(i).getDichGia().getTen_DichGia();

		}
	}

	public Sach getSach() {
		return sach;
	}
	@Command
	public void addGioHang(@BindingParam("id") Sach sach) {
		Integer idSach = sach.getId_Sach();
		String tenSach = sach.getTenSach();
		@SuppressWarnings("unchecked")
		HashMap<Integer, Sach> hashMapGioHang = (HashMap<Integer, Sach>) Sessions
				.getCurrent().getAttribute("gioHang");
		if (hashMapGioHang == null) {
			hashMapGioHang = new HashMap<Integer, Sach>();
			hashMapGioHang.put(idSach, sach);

		} else {
			hashMapGioHang.put(idSach, sach);
		}

		// Chuyển trang truyền tham số
		Sessions.getCurrent().setAttribute("gioHang", hashMapGioHang);
		Executions.sendRedirect("shoppingcart.zul?target=chitietgiohang");
	}


}
